/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.types;

import de.tor.tribes.reportserver.PersistenceImpl;
import de.tor.tribes.reportserver.util.ReportFormatter;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * @author Torridity
 */
@Entity
@Table(name = "ReportAccessGrants")
public class ZebraReportAccessGrant {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @OneToOne
    private ZebraUser user;
    @OneToOne
    private ZebraReport report;
    @OneToOne(mappedBy = "grant", cascade = CascadeType.ALL)
    private ZebraReportGroupMapping groupMapping;
    private boolean shareSource;
    private boolean shareAttackers;
    private boolean shareAttackerLosses;
    private boolean shareTarget;
    private boolean shareDefenders;
    private boolean shareDefenderLosses;
    private boolean shareHaul;
    private boolean shareSpyInfo;
    private boolean owner;

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the user
     */
    public ZebraUser getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(ZebraUser user) {
        this.user = user;
    }

    /**
     * @return the report
     */
    public ZebraReport getReport() {
        return report;
    }

    /**
     * @param report the report to set
     */
    public void setReport(ZebraReport report) {
        this.report = report;
    }

    public void setOwner(boolean owner) {
        this.owner = owner;
        if (owner) {
            shareSource = true;
            shareAttackers = true;
            shareAttackerLosses = true;
            shareTarget = true;
            shareDefenders = true;
            shareDefenderLosses = true;
            shareHaul = true;
            shareSpyInfo = true;
        }
    }

    public boolean isOwner() {
        return owner;
    }

    /**
     * @return the shareSource
     */
    public boolean isShareSource() {
        return shareSource;
    }

    /**
     * @param shareSource the shareSource to set
     */
    public void setShareSource(boolean shareSource) {
        this.shareSource = shareSource;
    }

    /**
     * @return the shareAttackers
     */
    public boolean isShareAttackers() {
        return shareAttackers;
    }

    /**
     * @param shareAttackers the shareAttackers to set
     */
    public void setShareAttackers(boolean shareAttackers) {
        this.shareAttackers = shareAttackers;
    }

    /**
     * @return the shareAttackerLosses
     */
    public boolean isShareAttackerLosses() {
        return shareAttackerLosses;
    }

    /**
     * @param shareAttackerLosses the shareAttackerLosses to set
     */
    public void setShareAttackerLosses(boolean shareAttackerLosses) {
        this.shareAttackerLosses = shareAttackerLosses;
    }

    /**
     * @return the shareTarget
     */
    public boolean isShareTarget() {
        return shareTarget;
    }

    /**
     * @param shareTarget the shareTarget to set
     */
    public void setShareTarget(boolean shareTarget) {
        this.shareTarget = shareTarget;
    }

    /**
     * @return the shareDefenders
     */
    public boolean isShareDefenders() {
        return shareDefenders;
    }

    /**
     * @param shareDefenders the shareDefenders to set
     */
    public void setShareDefenders(boolean shareDefenders) {
        this.shareDefenders = shareDefenders;
    }

    /**
     * @return the shareDefenderLosses
     */
    public boolean isShareDefenderLosses() {
        return shareDefenderLosses;
    }

    /**
     * @param shareDefenderLosses the shareDefenderLosses to set
     */
    public void setShareDefenderLosses(boolean shareDefenderLosses) {
        this.shareDefenderLosses = shareDefenderLosses;
    }

    /**
     * @return the shareHaul
     */
    public boolean isShareHaul() {
        return shareHaul;
    }

    /**
     * @param shareHaul the shareHaul to set
     */
    public void setShareHaul(boolean shareHaul) {
        this.shareHaul = shareHaul;
    }

    /**
     * @return the shareSpyInfo
     */
    public boolean isShareSpyInfo() {
        return shareSpyInfo;
    }

    /**
     * @param shareSpyInfo the shareSpyInfo to set
     */
    public void setShareSpyInfo(boolean shareSpyInfo) {
        this.shareSpyInfo = shareSpyInfo;
    }

    /**
     * @return the group
     */
    public ZebraReportGroupMapping getGroupMapping() {
        return groupMapping;
    }

    /**
     * @param group the group to set
     */
    public void setGroupMapping(ZebraReportGroupMapping group) {
        this.groupMapping = group;
    }

    public void delete(PersistenceImpl pPersistence) {
        if (getGroupMapping() != null) {
            ZebraReportGroup group = getGroupMapping().getGroup();
            group.removeReport(this, pPersistence);
        }
        pPersistence.remove(this);
    }

    public String getLuckBar() {
        return ReportFormatter.buildLuckBar(getReport().getLuck());
    }
}
